function catchRain(height) {
    // 获取max
    let max = 0
    let maxIndex = 0
    height.forEach((item, index) => {
        if (max < item) {
            max = item
            maxIndex = index
        }
    });


    let sum = 0
    sum += count(maxIndex, height, true)
    sum += count(maxIndex, height, false)
    console.log(sum)
    return sum
}

function count(maxIndex, height, flag) {
    let rightStack = []
    let sum = 0
    tempIndex = flag ? maxIndex - 1 : maxIndex + 1
    while (flag ? tempIndex >= 0 : tempIndex <= height.length - 1) {
        let item = height[tempIndex]
        let num = 0
        while (rightStack.length > 0 && rightStack[rightStack.length - 1].value <= item) {
            let tempObj = rightStack.pop()
            num = num + tempObj.num + tempObj.value
        }
        rightStack.push({ value: item, num: num, index: tempIndex })
        flag ? tempIndex-- : tempIndex++
    }
    while (rightStack.length > 0) {
        let item = rightStack.pop()
        let temp = rightStack.length !== 0 ? rightStack[rightStack.length - 1] : { index: maxIndex }
        sum += item.value * (Math.abs(item.index - temp.index) - 1) - item.num
    }
    return sum
}

catchRain([4, 3, 5, 3, 4, 5, 4, 6, 3, 5, 1])